<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台内容管理ajax
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

class ajax_content {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //保存文章评论信息
                    $this->addArticleComment();
                    break;

                case 2: //查询文章评论信息
                    $this->searchArticleComment();
                    break;

                case 3: //查询底部活动
                    $this->searchContentCenter();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                        returnJsonData('参数错误');

                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->addArticleComment();//新增、修改

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteDistributionTechnician();//删除

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->changeSendTypeUseflg(); //启用/禁用

                    }

                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }


    /**
     * 保存文章评论信息
     */
    private function addArticleComment() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            $this->toReturn('请先登录', 404);
        }

        //检查验证码
        if (is_null($G_SHOP->sessionCache_get('yzm'))) {
            $this->toReturn('验证码已失效');
        } elseif ($G_SHOP->sessionCache_get('yzm') != md5(strtolower($_REQUEST['yzm']))) {
            $this->toReturn('验证码错误');
        }
        $G_SHOP->sessionCache_remove('yzm');

        //实例化内容类
        $contentHandler = new contentHandler( $G_SHOP->DBCA );

        //添加商品咨询内容
        $_REQUEST['pcustID'] = $G_SHOP->currentPCustID;
        $_REQUEST['creator'] = $G_SHOP->currentCustomerID;
        $result_array = $contentHandler->addArticleComment( $_REQUEST );

        $this->toReturn($result_array['message'], $result_array['code']);

    }

    /**
     * 查询文章评论信息
     */
    public function searchArticleComment() {

        if (!isset($_REQUEST['article_id']) || !is_numeric($_REQUEST['article_id'])) {
            $this->toReturn('参数错误');
        }

        global $G_SHOP;

        if (!isset($_REQUEST['page']) || !is_numeric($_REQUEST['page']) || $_REQUEST['page'] <= 0) {
            $_REQUEST['page'] = 1;
        }

        $arg_array = array(
            'article_id' => $_REQUEST['article_id'],
            'sortname' => 'article_id',
            'sortorder' => 'DESC',
            'page' => $_REQUEST['page'],
            'rp' => 10,
        );

        //实例化内容类
        $contentCenter = new contentCenter( $G_SHOP->DBCA );

        $return_array['article'] = $contentCenter->searchArticleComments( $arg_array );

        if ($return_array['article']) {

            //实例化公共类
            $commHandler = new commHandler();

            $return_array['paging'] = $commHandler->shopPage( $arg_array['page'], $arg_array['count'], $arg_array['rp'], 'javascript: G.article.comment.search(%d);', $G_SHOP->smarty);

        }

        $this->toReturn('ok', 200, $return_array);


    }

    /**
     * 按城市更新底部活动页缓存
     * add by allen.qiang 2015-05-26
     */

    public function searchContentCenter(){

        global $G_SHOP;

        $cityID = $G_SHOP->get_cookie_info("EBSIG_CITY_ID");

        $content_center_array = $G_SHOP->sessionCache_get('comm_content_center_'.$cityID, true);

        returnJsonData('cache', 200, $content_center_array);

    }
}